#include "mychartview.h"
#include <QDebug>
#include "QValueAxis"

MyChartView::MyChartView(QChart *chart, QWidget *parent): QChartView(chart, parent) {

    isZoomed = false;
    zoomTimer = new QTimer(this);
    zoomTimer->setInterval(10000);

    connect(zoomTimer,SIGNAL(timeout()),this,SLOT(resetZoom()));
}

void MyChartView::mousePressEvent(QMouseEvent *event)
{
    // qDebug() << "CHARTVIEW EVENT" << event;
    QPointF curVal;

    if (!isZoomed) {
        if (event->button() == Qt::LeftButton)
        {
            m_lastPoint = event->pos();

            curVal = this->chart()->mapToValue(QPointF(m_lastPoint));

            // this->chart()->axisX()->setRange(0.0, 20.0);
            this->chart()->axisX()->setRange(curVal.x()-75.0, curVal.x()+75.0);
        }

        isZoomed = true;
        zoomTimer->start();
    } else {
        // this->chart()->axisX()->setRange(400.0, 6000.0);
        // isZoomed = false;
        if(event->button() == Qt::LeftButton) {
            m_lastPoint = event->pos();
            m_isPress = true;
        }

    }

    qDebug() << "CHARTVIEW EVENT" << m_lastPoint << m_isPress << curVal;

}

void MyChartView::mouseReleaseEvent(QMouseEvent *event)
{
    // qDebug() << "CHARTVIEW RELEASE EVENT" << event;
    m_isPress = false;
    // if (event->button() == Qt::RightButton)
    // {
        // if (m_alreadySaveRange)
        // {
            // this->chart()->axisX()->setRange(0.0, 20.0);
            // QValueAxis *axisX = dynamic_cast<QValueAxis*>(this->chart()->axisX());
            // const double yMin = axisX->min();
            // const double yMax = axisX->max();
            // qDebug() << yMin << yMax;
            // this->chart()->axisX()->setRange(yMin, yMax);
        // }
    // }
    // isZoomed = false;
}

void MyChartView::mouseMoveEvent(QMouseEvent *event)
{
    // qDebug() << "CHARTVIEW MOVE EVENT" << event;
    // if (!m_coordItem)
    // {
    //     m_coordItem = new QGraphicsSimpleTextItem(this->chart());
    //     m_coordItem->setZValue(5);
    //     m_coordItem->setPos(100, 60);
    //     m_coordItem->show();
    // }
    if(isZoomed) {
        const QPoint curPos = event->pos();
        zoomTimer->stop();
        zoomTimer->start();

        QPointF curVal = this->chart()->mapToValue(QPointF(curPos));
        QString coordStr = QString("X = %1, Y = %2").arg(curVal.x()).arg(curVal.y());

        // m_coordItem->setText(coordStr);
        qDebug() << "COORD" << coordStr;
        qDebug() << curPos << m_lastPoint;
        if (m_isPress)
        {
            QPoint offset = curPos - m_lastPoint;
            m_lastPoint = curPos;
            if (!m_alreadySaveRange)
            {
                this->saveAxisRange();
                m_alreadySaveRange = true;
            }
            this->chart()->scroll(-offset.x(), 0);
        }
    }
}

void MyChartView::saveAxisRange()
{
    // this->chart()
}

void MyChartView::resetZoom()
{
    isZoomed = false;
    zoomTimer->stop();
    this->chart()->axisX()->setRange(400.0, 6000.0);
    this->chart()->axisY()->setRange(0.0, 20.0);
}
